package com.appcheck.recording.handler.impl;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.ItemClickAction;
import com.appcheck.recording.action.impl.ItemSelectedAction;
import com.appcheck.recording.action.impl.ScrollListAction;
import com.appcheck.recording.action.impl.ScrollStateChangedAction;
import com.appcheck.recording.action.impl.TouchAction;
import com.appcheck.recording.handler.action.SpinnerAction;
import com.appcheck.recording.handler.action.SpinnerItemSelectedAction;

public class SpinnerGrouper extends Grouper {

	private Integer indexOfCurrentWindow;
	public SpinnerGrouper(Grouper paramGrouper) {
		super(paramGrouper);
	}
	
	public final boolean visit(TouchAction action){
		View v = action.getView();
		if(action.getActionCode() == android.view.MotionEvent.ACTION_UP){
			if((v instanceof Spinner)){
				//这里只是记录了一下点击了这个Spinner，没有产生Action
				this.indexOfCurrentWindow = Integer.valueOf(WidgetHelper.getCurrentIndexOfView(action.getView()));
			}else if((!(v instanceof AdapterView)) && (this.indexOfCurrentWindow != null)){
				this.indexOfCurrentWindow = null;
			}
		}
		return false;
	}
	
	public final boolean visit(ItemClickAction action){
		if(this.indexOfCurrentWindow != null){
			SpinnerAction spinnerAction = new SpinnerAction();
			spinnerAction.setIndex(this.indexOfCurrentWindow.intValue());
			spinnerAction.setLine(action.getLine());
			forwardActionToPlugin(spinnerAction);
			this.indexOfCurrentWindow = null;
			return true;
		}
		return false;
	}
	
	public final boolean visit(ItemSelectedAction action){
		if(this.indexOfCurrentWindow != null){
			SpinnerItemSelectedAction spinnerAction = new SpinnerItemSelectedAction();
			spinnerAction.setIndex(this.indexOfCurrentWindow.intValue());
			spinnerAction.setLine(action.getLine());
			forwardActionToPlugin(spinnerAction);
			this.indexOfCurrentWindow = null;
			return true;
		}
		return false;
	}
	
	public final boolean visit(ScrollListAction action){
		if(this.indexOfCurrentWindow != null){
			return true;
		}
		return false;
	}
	
	public final boolean visit(ScrollStateChangedAction action){
		if(this.indexOfCurrentWindow != null){
			return true;
		}
		return false;
	}
	
}
